package LeetCode;

/**
 * @Author: Dijah
 * @Date: 2023/6/30 17:48
 * @Desc:
 * 前缀和
 * https://leetcode.cn/problems/range-sum-query-immutable/
 */
public class code303_NumArray {

    private static int[] preSum;

    public code303_NumArray(int [] nums){
        preSum = new int[nums.length + 1];
        for (int i =1; i < preSum.length; i++) {
            preSum[i] = preSum[i-1] + nums[i-1];
        }
    }

    public static int sumRange(int left,int right){
        return preSum[right+1] - preSum[left];
    }

    public static void main(String[] args) {
        int [] nums = {1,3,4,5,6,4};
        code303_NumArray numArray = new code303_NumArray(nums);
        int i = sumRange(1, 2);
        System.out.println(i);
    }

}
